package com.jinzo.mporaba;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
//import android.util.Log;
import android.widget.RemoteViews;

// Rewrite this.
public class MonitorWidget extends AppWidgetProvider {
	 public static final String REFRESH="com.jinzo.mporaba.REFRESH_WIDGET";
	 
	 @Override
	 public void onReceive(Context ctxt, Intent intent) {
		 if (REFRESH.equals(intent.getAction())) {
			 RemoteViews rv = buildUpdate(ctxt);
			 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctxt);
			 ComponentName monitorWidget = new ComponentName(ctxt, MonitorWidget.class);
			 appWidgetManager.updateAppWidget(monitorWidget, rv);
		 } else {
			 super.onReceive(ctxt, intent);
		 }
	 }
	 
	@Override
	public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds ) {
		ComponentName monitorWidget;
		RemoteViews remoteViews = buildUpdate(context);
		monitorWidget = new ComponentName(context, MonitorWidget.class);
		appWidgetManager.updateAppWidget(monitorWidget, remoteViews);
	}
	
	public RemoteViews buildUpdate(Context context) {
		RemoteViews remoteViews;
		remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        String v = pref.getString("widget_vsebina","ERR");
        if (v == "ERR" || v == "") {
        	remoteViews.setTextViewText(R.id.widget_first_description, "Pred prvo uporabo je potrebno v nastavitvah določiti 3 podatke za prikaz.");
        	/* we need to reset everything */
        	remoteViews.setTextViewText(R.id.widget_first_value, "");
        	remoteViews.setTextViewText(R.id.widget_second_description, "");
        	remoteViews.setTextViewText(R.id.widget_second_value, "");
        	remoteViews.setTextViewText(R.id.widget_third_description, "");
        	remoteViews.setTextViewText(R.id.widget_third_value, "");
        	//Log.i("widget", "Empty or Error widget_vsebina preference");
        	return remoteViews;
        }
        String[] p = ListPreferenceMultiSelect.parseStoredValue(v);
        //Log.i("widget", "Got preferences:"+ p.toString());
		ContentResolver cr = context.getContentResolver();
		if (p.length == 0) {
			// no selection || first run
			Cursor foo = cr.query(QuotaItem.CONTENT_URI, null, null, null, null);
			if (foo.getCount() == 0) {
				remoteViews.setTextViewText(R.id.widget_first_description, "Pred prvo uporabo je potrebno osvežiti podatke.");
			} else {
				for (int z=0; z<3;z++) {
					if (foo.moveToFirst()) {	
						/* TODO: ugleh */
						switch (z){
							case 0:
								remoteViews.setTextViewText(R.id.widget_first_description, foo.getString(foo.getColumnIndex(DBAdapter.KEY_DISPLAYNAME)));
								if (foo.getInt(foo.getColumnIndex(DBAdapter.KEY_PREPAID)) == 1) {
									remoteViews.setTextViewText(R.id.widget_first_value, foo.getString(foo.getColumnIndex(DBAdapter.KEY_USED)) + "/" + foo.getString(foo.getColumnIndex(DBAdapter.KEY_AVAILABLE)) +" "+  foo.getString(foo.getColumnIndex(DBAdapter.KEY_UNIT)));
								} else {
									remoteViews.setTextViewText(R.id.widget_first_value, foo.getString(foo.getColumnIndex(DBAdapter.KEY_USED)) +" "+  foo.getString(foo.getColumnIndex(DBAdapter.KEY_UNIT)));
								}
								break;
							case 1:
								remoteViews.setTextViewText(R.id.widget_second_description, foo.getString(foo.getColumnIndex(DBAdapter.KEY_DISPLAYNAME)));
								if (foo.getInt(foo.getColumnIndex(DBAdapter.KEY_PREPAID)) == 1) {
									remoteViews.setTextViewText(R.id.widget_second_value, foo.getString(foo.getColumnIndex(DBAdapter.KEY_USED)) + "/" + foo.getString(foo.getColumnIndex(DBAdapter.KEY_AVAILABLE)) +" "+  foo.getString(foo.getColumnIndex(DBAdapter.KEY_UNIT)));
								} else {
									remoteViews.setTextViewText(R.id.widget_second_value, foo.getString(foo.getColumnIndex(DBAdapter.KEY_USED)) +" "+  foo.getString(foo.getColumnIndex(DBAdapter.KEY_UNIT)));
								}
								break;
							case 2:
								remoteViews.setTextViewText(R.id.widget_third_description, foo.getString(foo.getColumnIndex(DBAdapter.KEY_DISPLAYNAME)));
								if (foo.getInt(foo.getColumnIndex(DBAdapter.KEY_PREPAID)) == 1) {
									remoteViews.setTextViewText(R.id.widget_third_value, foo.getString(foo.getColumnIndex(DBAdapter.KEY_USED)) + "/" + foo.getString(foo.getColumnIndex(DBAdapter.KEY_AVAILABLE)) +" "+  foo.getString(foo.getColumnIndex(DBAdapter.KEY_UNIT)));
								} else {
									remoteViews.setTextViewText(R.id.widget_third_value, foo.getString(foo.getColumnIndex(DBAdapter.KEY_USED)) +" "+  foo.getString(foo.getColumnIndex(DBAdapter.KEY_UNIT)));
								}
								break;
							default:
								//Log.e("widget", "More than 3 values selected for display");
								break;
						}
					foo.moveToNext();
					}
				}
			}
		} else {
		int looper = p.length;
		if (looper > 3) {
			looper = 3;
		}
		/* we reset everything to start off clean*/
		remoteViews.setTextViewText(R.id.widget_first_description, "");
    	remoteViews.setTextViewText(R.id.widget_first_value, "");
    	remoteViews.setTextViewText(R.id.widget_second_description, "");
    	remoteViews.setTextViewText(R.id.widget_second_value, "");
    	remoteViews.setTextViewText(R.id.widget_third_description, "");
    	remoteViews.setTextViewText(R.id.widget_third_value, "");
		for (int i = 0; i<looper; i++) {
			Cursor c = cr.query(QuotaItem.CONTENT_URI, null, " _ID = \""+ p[i] +"\"", null, null);		
			if (c.moveToFirst()) {	
				/* TODO: ugleh */
				switch (i){
					case 0:
						remoteViews.setTextViewText(R.id.widget_first_description, c.getString(c.getColumnIndex(DBAdapter.KEY_DISPLAYNAME)));
						if (c.getInt(c.getColumnIndex(DBAdapter.KEY_PREPAID)) == 1) {
							remoteViews.setTextViewText(R.id.widget_first_value, c.getString(c.getColumnIndex(DBAdapter.KEY_USED)) + "/" + c.getString(c.getColumnIndex(DBAdapter.KEY_AVAILABLE)) +" "+  c.getString(c.getColumnIndex(DBAdapter.KEY_UNIT)));
						} else {
							remoteViews.setTextViewText(R.id.widget_first_value, c.getString(c.getColumnIndex(DBAdapter.KEY_USED)) +" "+  c.getString(c.getColumnIndex(DBAdapter.KEY_UNIT)));
						}
						break;
					case 1:
						remoteViews.setTextViewText(R.id.widget_second_description, c.getString(c.getColumnIndex(DBAdapter.KEY_DISPLAYNAME)));
						if (c.getInt(c.getColumnIndex(DBAdapter.KEY_PREPAID)) == 1) {
							remoteViews.setTextViewText(R.id.widget_second_value, c.getString(c.getColumnIndex(DBAdapter.KEY_USED)) + "/" + c.getString(c.getColumnIndex(DBAdapter.KEY_AVAILABLE)) +" "+  c.getString(c.getColumnIndex(DBAdapter.KEY_UNIT)));
						} else {
							remoteViews.setTextViewText(R.id.widget_second_value, c.getString(c.getColumnIndex(DBAdapter.KEY_USED)) +" "+  c.getString(c.getColumnIndex(DBAdapter.KEY_UNIT)));
						}
						break;
					case 2:
						remoteViews.setTextViewText(R.id.widget_third_description, c.getString(c.getColumnIndex(DBAdapter.KEY_DISPLAYNAME)));
						if (c.getInt(c.getColumnIndex(DBAdapter.KEY_PREPAID)) == 1) {
							remoteViews.setTextViewText(R.id.widget_third_value, c.getString(c.getColumnIndex(DBAdapter.KEY_USED)) + "/" + c.getString(c.getColumnIndex(DBAdapter.KEY_AVAILABLE)) +" "+  c.getString(c.getColumnIndex(DBAdapter.KEY_UNIT)));
						} else {
							remoteViews.setTextViewText(R.id.widget_third_value, c.getString(c.getColumnIndex(DBAdapter.KEY_USED)) +" "+  c.getString(c.getColumnIndex(DBAdapter.KEY_UNIT)));
						}
						break;
					default:
						//Log.e("widget", "More than 3 values selected for display");
						break;
				}
			} else {
				//Log.e("widget", "User has selected a nonexsisting data row");
			}
			c.close();
		}
		}
	return remoteViews;
	}
}